<!DOCTYPE html>
<html xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.7.3/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>

<body>
<div class="layuimini-container" id="vue">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">昵称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="userName" autocomplete="off" class="layui-input" id="userName">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="button" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>



    </div>
</div>
<!--<script type="text/html" id="sexTpl">-->
<!--    &lt;!&ndash; 这里的 checked 的状态只是演示 &ndash;&gt;-->
<!--    <span class="">${d.sex} == true ? '男' : '女'</span>-->
<!--</script>-->
<script type="text/html" id="currentTableBar" lay-filter="currentTableFilter">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
</script>
<script src="../lib/layui-v2.7.3/layui/layui.js" charset="utf-8"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../js/vue.js"></script>
<script src="../api/urlConfig.js"></script>
<script>


    layui.use(['table', 'util'], function(){
        var table = layui.table
            ,util = layui.util;

        //监听单元格编辑
        table.on('edit(currentTableId)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
        });
    });
    function listData(){
        $.ajax({
            url: ipURL + "/authorize/user/list",
            headers: {
                "token": localStorage.token
            },
            type: 'get',
            // dataType: 'json',

        })
    }
    layui.use(['form', 'table','upload'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        table.render({
            elem: '#currentTableId',
            // url: '../api/table.json',	
			url:ipURL+"/admin/user/list",
            headers:{token:localStorage.token},
			parseData: function(res){ //res 即为原始返回的数据
			    return {
			      "code": res.code, //解析接口状态
			      "msg": res.message, //解析提示文本
			      "count": res.count, //解析数据长度
			      "data": res.data,//解析数据列表
				  
			    };	
				
			},
			
			response: {
			            statusCode: 0//规定成功的状态码，默认：0
			        },
			text:{none: '无数据'},
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 80, title: 'ID', sort: true,hide:true},
                {field: 'nickName', width: 150, title: '昵称'},
                {field: 'sex', width: 80, title: '性别',templet: function(d){
                        return d.sex ? '男' : '女';
                    }},
                {field: 'email', width: 150, title: '邮箱'},
                {field: 'password', width: 150, title: '密码',templet: function(d){
            return '*********';
        }},
                {field: 'description', width: 210, title: '描述'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });
        //编辑删除
        table.on('tool(currentTableFilter)', function(obj){
            var datas = obj.data; // 获取当前行的数据
            var layEvent = obj.event; // 获取 lay-event 对应的值

            if(layEvent === 'edit'){
                // 编辑操作
                console.log('编辑操作', datas);
                // 执行编辑逻辑，例如弹出编辑表单
                layer.open({
                    type: 1,
                    title: '编辑用户信息',
                    content: $('#save'), // 这里是表单的id
                    area: ['850px', '500px'],
                    // btn: ['退出'],
                    end: function () {
                        $('#save').css('display', 'none')
                    },
                    success: function(layero, index){
                        // 表单赋值
                        form.val('stuform', datas);
                    }
                });
                // 表单提交事件
                form.on('submit(editFormSubmit)', function(data){
                    // 发送请求到服务器进行验证和更新
                    $.ajax({
                        url:ipURL+"/admin/user/update",
                        type: 'post',
                        data: JSON.stringify(data.field),
                        dataType: 'JSON',
                        contentType: 'application/json;charset=UTF-8',
                        beforeSend: function (request) {
                            request.setRequestHeader("token", localStorage.token);
                        },
                        success: function(res) {
                        //    if(data.field.oldPassword!=datas.password){
                        //        layer.msg('旧密码错误不一致！');
                        // }
                            // if(res.code == 0){
                                layer.msg(res.msg||'更新成功');
                                layer.closeAll('page'); // 关闭所有页面层
                            table.reload('currentTableId', {
                                data: res.data, // 更新后的数据
                            });
                            // } else {
                            //     layer.msg(res.msg); // 显示错误消息
                            // }
                        },
                        error: function(res) {
                            layer.msg(res.msg||'更新失败');
                        }
                    });
                    return false; // 阻止表单默认提交
                });
            } else if(layEvent === 'delete'){
                // 执行删除逻辑，例如弹出确认框
                layer.confirm('确定删除选中的用户吗?', function(index){
                // layer.confirm('确定删除嘛？', function(index){
                    // 发送删除请求
                    $.ajax({
                        url: ipURL + '/admin/user/delete/'+datas.id,
                        method: 'get',
                        headers:{token:localStorage.token},
                        success: function (res) {
                            layer.msg(res.message);
                            layer.close(index);
                            // table.render({elem: '#stuform', // 指定表格的 id 或 class
                            //     data: res.data,});
                            table.reload('currentTableId', {
                                data: res.data, // 更新后的数据
                            });
                        },
                        error: function (data) {
                            layer.msg(data.message||"出现异常，联系管理员");
                        }
                    });
                    layer.close(index);
                });
            }
        });
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
			
			var userName = $('input[name="userName"]').val()
			var email = $('input[name="email"]').val()
            //执行搜索重载
            table.reload('currentTableId', {
				
                page: {
                    curr: 1
                }
                , where: {
                    userName:userName,
                    email:email
                }
            });

            return false;
        });
    });
</script>

</body>
<style type="text/css">
    .myData .layui-form-item {
        margin: 20px 100px 10px 45px;
    }

    .myData .layui-form-label {
        width: 80px;
    }
</style>
<form class="layui-form myData" action="save" method="post" lay-filter="stuform" id="save" style="display: none">
    <input type="hidden" name="id"/>
    <div class="layui-form-item">
        <label class="layui-form-label">昵称:</label>
        <div class="layui-input-block">
            <input type="text" name="nickName" class="layui-input"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">性别:</label>
        <div class="layui-input-block">
            <input type="text" name="sex" class="layui-input"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码:</label>
        <div class="layui-input-block">
            <input type="password" name="password" autocomplete="new-password" class="layui-input"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">描述:</label>
        <div class="layui-input-block">
            <textarea name="description" class="layui-textarea">
            在这里输入多行文本...
            </textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" lay-submit lay-filter="editFormSubmit">提交</button>
        </div>
    </div>
</form>
</html>